package com.myk.game.heroscuffle.heroes;

import com.myk.game.heroscuffle.game.Attacked;
import com.myk.game.heroscuffle.game.Element;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.HeroPool;
import com.myk.game.heroscuffle.utils.RdmPropUtil;

public class Hero_FeiLian extends Hero {

    private static final int BASE_BACK_DAMAGE = 10; //基础反伤数值
    private int beAttackedCount = 0; //本回合被攻击次数统计

    public Hero_FeiLian() {
        super("飞镰",
                "攻击时有几率临时增加目标防御力数值或一半的攻击力。被攻击时可以反击一定伤害，且回合内被攻击的次数越多反击的伤害越高。",
                Element.THUNDER,
                1120, 160, 42);
    }

    @Override
    public void onAutoSelectAction() {
        //优先攻击防御力高的敌人
        Attacked attacked = autoSelectAttacked();
        Hero pre = attacked.getTarget(); //默认随机选择的英雄
        Hero best = game.pool.getBestAliveHeroBy((challenged, challenger) -> Integer.compare(challenger.def, challenged.def), this); //防御力最高的英雄
        if (pre == best) {
            attacked.setReason("精心瞄准突袭");
            doAttackActionStart(attacked);
        }
        else {
            if (RdmPropUtil.bingo(2 / 3d)) {
                doAttackActionStart(new Attacked(best, "精心瞄准突袭"));
            }
            else {
                doAttackActionStart(attacked);
            }
        }
    }

    @Override
    public void onPreAction() {
        beAttackedCount = 0; //回合开始的时候重置被攻击次数统计
    }

    @Override
    public void onBeforeDoAttack(Hero attacked) {
        //普通攻击有几率临时增加攻击力
        if (RdmPropUtil.bingo(2, 3)) {
            if (RdmPropUtil.bingo(2, 3)) { 
                int atkAdd = attacked.def / 2; //攻击力临时增加的量为目标防御力的一半
                game.ui.printMsg("发现破绽" + getObviousName() + "atk临时增加" + atkAdd + "点，");
                atk += atkAdd;
                event.onAfterDoAttack.addRunBack(eventData -> {
                    atk -= atkAdd;
                    eventData.removeRunBackFromListener();
                });
            }
            else {
                int atkAdd = attacked.def; //攻击力临时增加的量为目标防御力
                game.ui.printMsg("发现致命破绽" + getObviousName() + "atk临时增加" + atkAdd + "点，");
                atk += atkAdd;
                event.onAfterDoAttack.addRunBack(eventData -> {
                    atk -= atkAdd;
                    eventData.removeRunBackFromListener();
                });
            }
        }
        attacked.beAttackActionStart(this); //重写记得回调此方法以继续流程
    }

    @Override
    public void onAfterBeAttack(Hero attacker, int damage) {
        beAttackedCount++;
        int backDamage = beAttackedCount * BASE_BACK_DAMAGE; //反击伤害
        game.ui.printMsg(attacker.getObviousName() + "被尖刃反刺");
        attacker.sufferDamage(null, null, null, backDamage);
    }
}
